<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>performancetimer_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_testing_performancetimer_test.js.html">performancetimer_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.testing.PerformanceTimerTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.testing.PerformanceTimerTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.async.Deferred&#39;);
<a name="line19"></a>goog.require(&#39;goog.dom&#39;);
<a name="line20"></a>goog.require(&#39;goog.math&#39;);
<a name="line21"></a>goog.require(&#39;goog.testing.MockClock&#39;);
<a name="line22"></a>goog.require(&#39;goog.testing.PerformanceTimer&#39;);
<a name="line23"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line24"></a>
<a name="line25"></a>var mockClock;
<a name="line26"></a>var sandbox;
<a name="line27"></a>var timer;
<a name="line28"></a>function setUpPage() {
<a name="line29"></a>  sandbox = document.getElementById(&#39;sandbox&#39;);
<a name="line30"></a>}
<a name="line31"></a>
<a name="line32"></a>function setUp() {
<a name="line33"></a>  mockClock = new goog.testing.MockClock(true);
<a name="line34"></a>  timer = new goog.testing.PerformanceTimer();
<a name="line35"></a>}
<a name="line36"></a>
<a name="line37"></a>function tearDown() {
<a name="line38"></a>  mockClock.dispose();
<a name="line39"></a>  timer = null;
<a name="line40"></a>  goog.dom.removeChildren(sandbox);
<a name="line41"></a>}
<a name="line42"></a>
<a name="line43"></a>function testConstructor() {
<a name="line44"></a>  assertTrue(&#39;Timer must be an instance of goog.testing.PerformanceTimer&#39;,
<a name="line45"></a>      timer instanceof goog.testing.PerformanceTimer);
<a name="line46"></a>  assertEquals(&#39;Timer must collect the default number of samples&#39;, 10,
<a name="line47"></a>      timer.getNumSamples());
<a name="line48"></a>  assertEquals(&#39;Timer must have the default timeout interval&#39;, 5000,
<a name="line49"></a>      timer.getTimeoutInterval());
<a name="line50"></a>}
<a name="line51"></a>
<a name="line52"></a>function testRun_noSetUpOrTearDown() {
<a name="line53"></a>  runAndAssert(false, false, false);
<a name="line54"></a>}
<a name="line55"></a>
<a name="line56"></a>function testRun_withSetup() {
<a name="line57"></a>  runAndAssert(true, false, false);
<a name="line58"></a>}
<a name="line59"></a>
<a name="line60"></a>function testRun_withTearDown() {
<a name="line61"></a>  runAndAssert(false, true, false);
<a name="line62"></a>}
<a name="line63"></a>
<a name="line64"></a>function testRun_withSetUpAndTearDown() {
<a name="line65"></a>  runAndAssert(true, true, false);
<a name="line66"></a>}
<a name="line67"></a>
<a name="line68"></a>function testRunAsync_noSetUpOrTearDown() {
<a name="line69"></a>  runAndAssert(false, false, true);
<a name="line70"></a>}
<a name="line71"></a>
<a name="line72"></a>function testRunAsync_withSetup() {
<a name="line73"></a>  runAndAssert(true, false, true);
<a name="line74"></a>}
<a name="line75"></a>
<a name="line76"></a>function testRunAsync_withTearDown() {
<a name="line77"></a>  runAndAssert(false, true, true);
<a name="line78"></a>}
<a name="line79"></a>
<a name="line80"></a>function testRunAsync_withSetUpAndTearDown() {
<a name="line81"></a>  runAndAssert(true, true, true);
<a name="line82"></a>}
<a name="line83"></a>
<a name="line84"></a>function runAndAssert(useSetUp, useTearDown, runAsync) {
<a name="line85"></a>  var fakeExecutionTime = [100, 95, 98, 104, 130, 101, 96, 98, 90, 103];
<a name="line86"></a>  var count = 0;
<a name="line87"></a>  var testFunction = function() {
<a name="line88"></a>    mockClock.tick(fakeExecutionTime[count++]);
<a name="line89"></a>    if (runAsync) {
<a name="line90"></a>      var deferred = new goog.async.Deferred();
<a name="line91"></a>      deferred.callback();
<a name="line92"></a>      return deferred;
<a name="line93"></a>    }
<a name="line94"></a>  };
<a name="line95"></a>
<a name="line96"></a>  var setUpCount = 0;
<a name="line97"></a>  var setUpFunction = function() {
<a name="line98"></a>    // Should have no effect on total time.
<a name="line99"></a>    mockClock.tick(7);
<a name="line100"></a>    setUpCount++;
<a name="line101"></a>    if (runAsync) {
<a name="line102"></a>      var deferred = new goog.async.Deferred();
<a name="line103"></a>      deferred.callback();
<a name="line104"></a>      return deferred;
<a name="line105"></a>    }
<a name="line106"></a>  };
<a name="line107"></a>
<a name="line108"></a>  var tearDownCount = 0;
<a name="line109"></a>  var tearDownFunction = function() {
<a name="line110"></a>    // Should have no effect on total time.
<a name="line111"></a>    mockClock.tick(11);
<a name="line112"></a>    tearDownCount++;
<a name="line113"></a>    if (runAsync) {
<a name="line114"></a>      var deferred = new goog.async.Deferred();
<a name="line115"></a>      deferred.callback();
<a name="line116"></a>      return deferred;
<a name="line117"></a>    }
<a name="line118"></a>  };
<a name="line119"></a>
<a name="line120"></a>  // Fast test function should complete successfully in under 5 seconds...
<a name="line121"></a>  var task = new goog.testing.PerformanceTimer.Task(testFunction);
<a name="line122"></a>  if (useSetUp) {
<a name="line123"></a>    task.withSetUp(setUpFunction);
<a name="line124"></a>  }
<a name="line125"></a>  if (useTearDown) {
<a name="line126"></a>    task.withTearDown(tearDownFunction);
<a name="line127"></a>  }
<a name="line128"></a>  if (runAsync) {
<a name="line129"></a>    var assertsRan = false;
<a name="line130"></a>    var deferred = timer.runAsyncTask(task);
<a name="line131"></a>    deferred.addCallback(
<a name="line132"></a>        function(results) {
<a name="line133"></a>          assertsRan = assertResults(results, useSetUp, useTearDown,
<a name="line134"></a>              setUpCount, tearDownCount, fakeExecutionTime);
<a name="line135"></a>        });
<a name="line136"></a>    assertTrue(assertsRan);
<a name="line137"></a>  } else {
<a name="line138"></a>    var results = timer.runTask(task);
<a name="line139"></a>    assertResults(results, useSetUp, useTearDown, setUpCount, tearDownCount,
<a name="line140"></a>        fakeExecutionTime);
<a name="line141"></a>  }
<a name="line142"></a>}
<a name="line143"></a>
<a name="line144"></a>function assertResults(results, useSetUp, useTearDown, setUpCount,
<a name="line145"></a>    tearDownCount, fakeExecutionTime) {
<a name="line146"></a>  assertNotNull(&#39;Results must be available.&#39;, results);
<a name="line147"></a>
<a name="line148"></a>  assertEquals(&#39;Average is wrong.&#39;,
<a name="line149"></a>      goog.math.average.apply(null, fakeExecutionTime), results[&#39;average&#39;]);
<a name="line150"></a>  assertEquals(&#39;Standard deviation is wrong.&#39;,
<a name="line151"></a>      goog.math.standardDeviation.apply(null, fakeExecutionTime),
<a name="line152"></a>      results[&#39;standardDeviation&#39;]);
<a name="line153"></a>
<a name="line154"></a>  assertEquals(&#39;Count must be as expected.&#39;, 10, results[&#39;count&#39;]);
<a name="line155"></a>  assertEquals(&#39;Maximum is wrong.&#39;, 130, results[&#39;maximum&#39;]);
<a name="line156"></a>  assertEquals(&#39;Mimimum is wrong.&#39;, 90, results[&#39;minimum&#39;]);
<a name="line157"></a>  assertEquals(&#39;Total must be a nonnegative number.&#39;,
<a name="line158"></a>      goog.math.sum.apply(null, fakeExecutionTime), results[&#39;total&#39;]);
<a name="line159"></a>
<a name="line160"></a>  assertEquals(&#39;Set up count must be as expected.&#39;,
<a name="line161"></a>      useSetUp ? 10 : 0, setUpCount);
<a name="line162"></a>  assertEquals(&#39;Tear down count must be as expected.&#39;,
<a name="line163"></a>      useTearDown ? 10 : 0, tearDownCount);
<a name="line164"></a>
<a name="line165"></a>  return true;
<a name="line166"></a>}
<a name="line167"></a>
<a name="line168"></a>function testTimeout() {
<a name="line169"></a>  var count = 0;
<a name="line170"></a>  var testFunction = function() {
<a name="line171"></a>    mockClock.tick(100);
<a name="line172"></a>    ++count;
<a name="line173"></a>  };
<a name="line174"></a>
<a name="line175"></a>  timer.setNumSamples(200);
<a name="line176"></a>  timer.setTimeoutInterval(2500);
<a name="line177"></a>  var results = timer.run(testFunction);
<a name="line178"></a>
<a name="line179"></a>  assertNotNull(&#39;Results must be available&#39;, results);
<a name="line180"></a>  assertEquals(&#39;Count is wrong&#39;, count, results[&#39;count&#39;]);
<a name="line181"></a>  assertTrue(&#39;Count must less than expected&#39;,
<a name="line182"></a>      results[&#39;count&#39;] &lt; timer.getNumSamples());
<a name="line183"></a>}
<a name="line184"></a>
<a name="line185"></a>function testCreateResults() {
<a name="line186"></a>  var samples = [53, 0, 103];
<a name="line187"></a>  var expectedResults = {
<a name="line188"></a>    &#39;average&#39;: 52,
<a name="line189"></a>    &#39;count&#39;: 3,
<a name="line190"></a>    &#39;maximum&#39;: 103,
<a name="line191"></a>    &#39;minimum&#39;: 0,
<a name="line192"></a>    &#39;standardDeviation&#39;: goog.math.standardDeviation.apply(null, samples),
<a name="line193"></a>    &#39;total&#39;: 156
<a name="line194"></a>  };
<a name="line195"></a>  assertObjectEquals(
<a name="line196"></a>      expectedResults,
<a name="line197"></a>      goog.testing.PerformanceTimer.createResults(samples));
<a name="line198"></a>}
</pre>


</body>
</html>
